package rcp.iphex2.ui.projectexplorer.commandhandler;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.handlers.HandlerUtil;

import rcp.iphex2.SharedIds;
import rcp.iphex2.model.AreaAnnotation;
import rcp.iphex2.model.Project;
import rcp.iphex2.ui.projectexplorer.ProjectExplorerView;


public class AddKnownSubstancesHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		String PROJECT_EXPLORER_VIEW_ID = SharedIds.PROJECT_EXPLORER_VIEW_ID;

		IViewPart projectExplorer = HandlerUtil.getActivePart(event).getSite().getPage().findView(PROJECT_EXPLORER_VIEW_ID);

		if (projectExplorer != null) {
			final ProjectExplorerView projectExplorerView = (ProjectExplorerView) projectExplorer;
			Project project = projectExplorerView.getActiveProject();

			IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
			List<Object> structuredSelectionList = selection.toList();
			for (Object object : structuredSelectionList) {
				if (object instanceof AreaAnnotation) {
					AreaAnnotation areaAnnotation = (AreaAnnotation) object;
					project.addAreaAnnotation(areaAnnotation);
				}
			}
			projectExplorerView.update(true);
		}

		return null;
	}

}
